<html lang="cn">
<head>
    <title>Stats</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script>
        let list = {};
        let count = 0;
        const urls = [
            "https://npm-api.ng8866.com/stats/1460/loadMap",
            "https://npm-api.ng8866.com/stats/1461/loadMap",
            "https://npm-api.ng8866.com/stats/1480/loadMap",
            "https://npm-api.ng8866.com/stats/1481/loadMap",
        ];

        function getData(url) {
            $.ajax({
                async: false,
                url: url,
                success: function (result) {
                    $.each(result, function (ind, ite) {
                        list[ind] = [];
                        $.each(ite, function (index, item) {
                            // console.log(index);
                            // console.log(item);
                            if (list[ind].hasOwnProperty(index)) {
                                list[ind][index] = list[ind][index] + item;
                            } else {
                                list[ind][index] = item;
                            }
                        });
                    });
                }
            })
        }

        function getAllData() {
            list = {};
            $.each(urls, function (index, item) {
                getData(item);
            });
            // console.log(list);
            let html = "";
            $.each(list, function (ind, ite1) {
                html += "<div><h3>" + ind + "</h3></div>";
                count = 0;
                console.log(ind);
                console.log(ite1);
                $.each(ite1, function (index, item) {
                    console.log(index);
                    html += "<div>" + index + " : " + item + "</div>";
                    count += item;
                });
                html += "<div>总计：" + count + "</div>";
            });
            $('#main').html(html);
        }

        var intervalId = setInterval(getAllData, 1000);

        // setTimeout(function () {
        //     clearInterval(intervalId);
        //     console.log("定时器已停止");
        // }, 50000);
        let list2 = {};
        let count2 = 0;
        const urls2 = [
            "https://npm-api.ng8866.com/stats/1460/countMap",
            "https://npm-api.ng8866.com/stats/1461/countMap",
            "https://npm-api.ng8866.com/stats/1480/countMap",
            "https://npm-api.ng8866.com/stats/1481/countMap",
        ];

        function getData2(url) {
            $.ajax({
                async: false,
                url: url,
                success: function (result) {
                    $.each(result, function (index, item) {
                        // console.log(index);
                        // console.log(item);
                        if (list2.hasOwnProperty(index)) {
                            list2[index] = list2[index] + item;
                        } else {
                            list2[index] = item;
                        }
                    });
                }
            })
        }

        function getAllData2() {
            list2 = {};
            count2 = 0;
            $.each(urls2, function (index, item) {
                getData2(item);
            });
            // console.log(list2);
            let html = "";
            $.each(list2, function (index, item) {
                html += "<div>" + index + " : " + item + "</div>";
                count2 += item;
            });
            html += "<div>总计：" + count2 + "</div>";
            $('#main2').html(html);
        }

        var intervalId2 = setInterval(getAllData2, 1000);

        const urls3 = [
            "https://npm-api.ng8866.com/stats/1460/setCountMap?count=",
            "https://npm-api.ng8866.com/stats/1461/setCountMap?count=",
            "https://npm-api.ng8866.com/stats/1480/setCountMap?count=",
            "https://npm-api.ng8866.com/stats/1481/setCountMap?count=",
        ];

        function setCount(flag) {
            $.each(urls3, function (index, item) {
                let url = item + flag;
                $.ajax({
                    async: true,
                    url: url,
                    success: function (result) {
                        // alert(result.count);
                    }
                });
            });
        }

        $(document).ready(function () {
        });
    </script>
</head>
<body>
<div style="padding: 30px;">
    <h1>并发统计</h1>
    <div id="main">
        <div>test</div>
    </div>
    <h1>总数统计</h1>
    <div>
        <button onclick="setCount('true')">打开总数</button>
        <button onclick="setCount('false')">关闭总数</button>
    </div>
    <div id="main2">
        <div>test</div>
    </div>
</div>
</body>
</html>